package com.tos.uc.service.Impl;

import com.tos.uc.dao.ITosUserFeedbackDao;
import com.tos.uc.dao.IUserDao;
import com.tos.uc.domain.TosUser;
import com.tos.uc.domain.TosUserFeedback;
import com.tos.uc.domain.User;
import com.tos.uc.ecommons.IExecutors;
import com.tos.uc.ecommons.RegistHxThread;
import com.tos.uc.service.UserService;
import com.tos316.common.utils.SmsApiUtil;
import com.tos316.common.utils.oraginUtils.CommonUtil;
import com.tos316.common.utils.oraginUtils.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

/**
 * Created by yanfei on 2017/6/27.
 */
@Service
public class UserServiceImpl implements UserService {

    private TaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    @Autowired
    private IUserDao userDao;
    @Autowired
    private RegistHxThread registHxThread;
    @Autowired
    private ITosUserFeedbackDao tosUserFeedbackDao;

    private static final Logger Logger = LoggerFactory
            .getLogger(UserServiceImpl.class);

    @Override
    public User getUser(int id) {
        return userDao.getUserById(id);
    }

    @Override
    public TosUser login(String phoneNumber, String passWord) {
        TosUser user = userDao.checkLogin(phoneNumber, passWord);
        if (user != null) {
            String token = CommonUtil.generateUUIDString();
            userDao.updateToken(token, user.getId());
            user.setToken(token);
        }
        return user;
    }

    @Override
    public TosUser loginForCode(String mphone, String code) {
        TosUser user = userDao.checkCode(code, mphone);
        if (user != null) {
            String token = CommonUtil.generateUUIDString();
            userDao.updateToken(token, user.getId());
            user.setToken(token);
        }
        return user;

    }

    @Override
    public TosUser isExistPhone(String phoneNumber) {
        return userDao.checkUserExist(phoneNumber);
    }

    @Override
    public TosUser isExistNickname(String nickname) {
        return userDao.checkNicknameExist(nickname);
    }

    @Override
    public TosUser register(String phoneNumber, String password, String code, String nickName, Integer userFrom) {
        TosUser user = userDao.checkCode(code, phoneNumber);
        if (ObjectUtils.isNotNull(user)) {
            userDao.updateUserInfo(password, nickName, userFrom,
                    user.getId());
            String token = CommonUtil.generateUUIDString();
            userDao.updateToken(token, user.getId());
            user.setToken(token);
            user.setNickname(nickName);
            try {
                // 异步处理
                registHxThread.setUserId(user.getId());
                taskExecutor.execute(registHxThread);
//                IExecutors.executeThread(registHxThread);
            } catch (Exception e) {
                Logger.error(e.getMessage());
            }
        }
        return user;
    }

    @Override
    public TosUser checkCode(String mphone, String code) {
        return userDao.checkCode(code, mphone);
    }

    @Override
    public int updateUserPassword(String mphone, String password) {
        return userDao.updateUserPassword(mphone, password);
    }

    @Override
    public boolean checkUserExist(String mphone) {
        TosUser user = this.userDao.checkUserExist(mphone);
        if (user == null) {
            return false;
        } else if (user.getNickname() == null) {
            return false;
        } else {
            return true;
        }
    }


    @Override
    public String sendCode(int userId,String mphone, int type) {
        String code = CommonUtil.getRandomNumber(4);
        // TODO为了测试暂注掉，后面要打开
        SmsApiUtil.sendCode(mphone, code, type);

        String codeMd5 = CommonUtil.generateCode(code);

        if (type == 3) {// 更换手机的时候，不保存到user表
            userDao.updateUserCode(codeMd5, userId);
            return code;
        }
        // 如果用户存在
        TosUser user = this.userDao.checkUserExist(mphone);
        if (ObjectUtils.isNotNull(user)) {
            userDao.updateUserCode(codeMd5, user.getId());
        } else {
            TosUser u = new TosUser();
            u.setMphone(mphone);
            u.setCode(codeMd5);
            userDao.addUser(u);
            try {
                // 异步处理
                registHxThread.setUserId(u.getId());
                taskExecutor.execute(registHxThread);
//                IExecutors.executeThread(registHxThread);
            } catch (Exception e) {
                Logger.error(e.getMessage());
            }
        }
        return code;
    }

    @Override
    public int addUserFeedback(int type, int userId, int relateid, String content) {
        TosUserFeedback feedback = new TosUserFeedback();
        feedback.setUserid(userId);
        feedback.setType(type);
        feedback.setRelateid(relateid);
        feedback.setContent(content);
        return tosUserFeedbackDao.addUserFeedback(feedback);
    }

}
